import { Util } from '../../utils/util';
import { PermissionInfo } from '../menus/permissionInfo';
import { Engine } from '../engine';

export class Role {
    constructor({ id = '', name = '', remark = '', aPermission = null, fCodeList = [] } = {}) {
        this.id = id;
        this.name = name;
        this.remark = remark;
        this.fCodeList = [...fCodeList];
        this.permission = aPermission || new PermissionInfo();
        this._permissionStr = '';
        this.selected = false;
    }

    static clone(cloneData) {
        const cloned = new Role({
            id: cloneData.id,
            name: cloneData.name,
            remark: cloneData.remark,
            fCodeList: [...cloneData.fCodeList],
            aPermission: PermissionInfo.clone(cloneData.permission)
        });
        return cloned;
    }

    static empty() {
        return new Role();
    }

    static fromJson(json) {
        const id = Util.jsonToString(json['id']);
        const name = Util.jsonToString(json['roleName']);
        const remark = Util.jsonToString(json['remark']);
        const _permissionStr = Util.jsonToString(json['permission']);

        const role = new Role({ id, name, remark });
        role._permissionStr = _permissionStr;

        if (_permissionStr) {
            try {
                let jsonList = JSON.parse(_permissionStr);
                if (Array.isArray(jsonList)) {
                    role.fCodeList = jsonList;
                }
            } catch (e) {
                console.error('解析权限字符串失败:', e);
            }
        }



        return role;
    }

    // 获取角色信息，同时初始化权限
    static fromJsonAndInitWithOrgPermission(json) {
        let role = Role.fromJson(json);
        role.initPermissionInfo(
            PermissionInfo.clone(Engine.profile?.org?.orgPermissionInfo ?? new PermissionInfo()).menuTree);

       // console.log('Role.fromJsonAndInitWithOrgPermission:', role);
        return role;
    }



    static fromPlatformPkgRoleJson(json) {
        const id = Util.jsonToString(json['id']);
        const name = Util.jsonToString(json['roleName']);
        const remark = Util.jsonToString(json['remark']);
        const permissionList = json['permission'];

        const role = new Role({ id, name, remark });

        if (Array.isArray(permissionList)) {
            role.fCodeList = permissionList;
        }

        return role;
    }

    initPermissionInfo(menuTree) {
        if (this.fCodeList.length > 0) {
            const pInfo = new PermissionInfo();
            for (const item of menuTree) {
                if (item.clearUnselectedNode(this.fCodeList)) {
                    pInfo.menuTree.push(item);
                }
            }
            pInfo.initFunctionMenuPermissionMap();
            pInfo.getLeafTreeNode(this.fCodeList);
            this.permission = pInfo;
        }
    }

    toJson() {
        return {
            id: this.id,
            roleName: this.name,
            permission: JSON.stringify(this.permission.getLeafTreeNode()),
            remark: this.remark
        };
    }

    // 拷贝函数，与 fromJson 匹配
    // 用于将当前角色转换为 JSON 对象，便于复制或传输
    toCopyJson() {
        return {
            id: this.id,
            roleName: this.name,  // Matches fromJson's roleName
            remark: this.remark,
            permission: JSON.stringify(this.permission.getLeafTreeNode())  // Matches fromJson's permission handling
        };
    }

    toPlatformJson() {
        return {
            roleName: this.name,
            permission: this.permission.getLeafTreeNode([])
        };
    }
}
